//
//  FQPopWindowAction.m
//  KDFDApp
//
//  Created by Bruce on 2017/8/25.
//  Copyright © 2017年 cailiang. All rights reserved.
//

#import "FQPopWindowAction.h"
#import "UIView+Additions.h"
#import "FQWKWebResponseEntity.h"
#import "FQBaseWKWebViewEntity.h"

@interface FQPopWindowAction ()
@property (nonatomic, strong) NSMutableDictionary <NSString *, void(^)(FQWKWebResponseEntity *)>*map;
@property (nonatomic, weak) WKWebView *webView;
@end

@implementation FQPopWindowAction
-(NSDictionary<NSString *,void (^)(FQWKWebResponseEntity *)> *)map
{
    if (!_map) {
        _map = [@{} mutableCopy];
    }
    return _map;
}
- (void)action:(QCWKWebViewJSRequestEntity *)requestEntity andCallbackBlock:(void (^)(FQWKWebResponseEntity *))response
{
    if(requestEntity&&requestEntity.callback){
        [self.map setObject:[response copy] forKey:requestEntity.callback];
    }
    [self popWindow:requestEntity];
}
- (void)setWebView:(WKWebView *)webview
{
    _webView = webview;
}
#pragma mark - 出栈
- (void)popWindow:(QCWKWebViewJSRequestEntity *)request{
  
    if ([UIApplication sharedApplication].keyWindow.rootViewController.presentedViewController) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self pop:request];
        });
    }else{
         [self pop:request];
        }
}
- (void)pop:(QCWKWebViewJSRequestEntity *)request{
    UIViewController *vc = [UIApplication sharedApplication].windows[0];
    UINavigationController *nav;
    UIViewController *present;
    NSArray * childVCs = @[];
    if ([UIApplication sharedApplication].keyWindow.rootViewController.presentedViewController) {
        present = [UIApplication sharedApplication].keyWindow.rootViewController.presentedViewController;
        if ([present isKindOfClass:[UINavigationController class]]) {
            childVCs = [(UINavigationController *)present viewControllers];
            nav = (UINavigationController *)present;
        }
    }else{
        childVCs = [vc.navigationController childViewControllers];
        nav = vc.navigationController;
    }
    
    if (childVCs.count<=1) {
        return;
    }
    UIViewController *lastVC = [childVCs objectAtIndex:childVCs.count-2];
   
    NSMutableArray *newChildVCArr = [@[] mutableCopy];
    for (UIViewController *vc in childVCs) {
        UIViewController *tmp = _webView?_webView.viewController:nil;
        if(tmp&&[vc isEqual:tmp]){
            continue;
        }
        [newChildVCArr addObject:vc];
    }
    
    [nav setViewControllers:newChildVCArr];

    
    FQWKWebResponseEntity *response = [FQWKWebResponseEntity getResponse];
    response.callbackid = request.callback;
    response.data.code = ENUM_Succeed;
    response.data.message = @"succeed";
    response.data.data = @{};
    [self.map enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, void (^ _Nonnull obj)(FQWKWebResponseEntity *response), BOOL * _Nonnull stop) {
        if (key == request.callback) {
            obj(response);
        }
    }];
}
@end
